﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Giaiphongmatbang.DataAccess;

namespace Giaiphongmatbang.BLL
{
    public class LandRepository
    {
        private GPMBDataContext data = new GPMBDataContext();

        public IEnumerable<Land> GetAll(int pageSize, int pageNumber, ref int totalRow)
        {
            var results = from p in data.Lands                          
                          select p;
            totalRow = results.Count();
            return results.OrderBy(p => p.LandName).Skip((pageNumber - 1) * pageSize).Take(pageSize);
        }

        public Land GetByID(int ID)
        {
            return data.Lands.Where(p=>p.LandID==ID).FirstOrDefault();
        }

        public IEnumerable<Land> GetByCMND(string cmnd)
        {
            return data.Lands.Where(p => p.InventoryLands.Any(q => q.CMND == cmnd));
        }

        public IEnumerable<Land> GetByProjectID(int ID)
        {
            return data.Lands.Where(p => p.InventoryLands.Any(q => q.ProjectID == ID));
        }

   
        public void Insert(Land l)
        {
            data.Lands.InsertOnSubmit(l);
        }

        public bool DeleteByID(int id)
        {
            var obj = GetByID(id);
            if (obj != null)
            {
                try
                {
                    data.Lands.DeleteOnSubmit(obj);
                    data.SubmitChanges();
                    return true;
                }
                catch
                {
                    return false;
                }
            }
            return false;
        }

        public void Save()
        {
            data.SubmitChanges();
        }

    }
}